import { PcSeriveProvider } from './../../../../providers/pc-serive/pc-serive';
import { Component, ViewChild } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  Slides,
  ToastController,
  App,
  LoadingController
} from "ionic-angular";
import { TeamSeriveProvider } from "../../../../providers/team-serive/team-serive";
import { BaseUI } from "../../../../common/baseUI";

/**
 * Generated class for the TeamMyPage page.
 * 
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-team-my",
  templateUrl: "team-my.html"
})
export class TeamMyPage extends BaseUI {
  teamDatas: Array<any> = [];
  teamAllDatas: Array<any> = [];
  myCreateTeams: Array<any> = [];
  //加载提示框
  loading;
  //轮播图
  @ViewChild(Slides) slides: Slides;
  //当前选中第几个 默认0
  tabsActiveIndex = 0;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public teamSeriveProvider: TeamSeriveProvider,
    public toastCtrl: ToastController,
    public loadingCtrl: LoadingController,
    public app: App,
    private pcSerive: PcSeriveProvider,
  ) {
    super();
  }

  ionViewWillEnter() {
    //进入时显示正在加载
    this.loading = this.showLoading(this.loadingCtrl, "加载中");
    this.init();
    //给团队加背景图
    this.teamSeriveProvider.getTeamAll().subscribe(data => {
      let teamimg = data;
      //判断缓存的团队和服务器的团队是否一致，不一致的话push图片进去缓存
      if (localStorage.getItem('teamimg') && teamimg.length > JSON.parse(localStorage.getItem('teamimg')).length) {
        let imgdata = JSON.parse(localStorage.getItem('teamimg'));
        let img = {};
        let olddata = JSON.parse(localStorage.getItem('teamimg')).length;
        for (let index = olddata; index < teamimg.length; index++) {
          if (teamimg.length - 1 == index) {
            img = teamimg[index];
            img['teambg'] = this.pcSerive.rand();
            imgdata.push(img);
            localStorage.setItem('teamimg', JSON.stringify(imgdata));
          }
        }
      }
    });
  }

  init() {
    this.getTeamAll();
    this.getJoinTeam();
    this.getCreateMyTeams();
  }

  //调用接口 获取加入的团队 
  getJoinTeam() {
    this.teamSeriveProvider.getJoinTeam().subscribe(data => {
      if (data != null) {
        if (data.status != 500) {
          this.teamDatas = data;
          return;
        }
        if (data.status == 500) {
          this.loginTimeOut();
        }
      }
    }, err => {
      super.showToast(this.toastCtrl, "请检查网络连接之后重试");
    });
  }

  //调用接口 获取用户创建的团队
  getCreateMyTeams() {
    this.teamSeriveProvider.getMyCreateTeams().subscribe(data => {
      if (data != null) {
        if (data.status != 500) {
          if (data.status == 400) {
            super.showToast(this.toastCtrl, "您没有权限创建团队");
            return;
          }
          this.myCreateTeams = data;
          return;
        }
        if (data.status == 500) {
          this.loginTimeOut();
        }
      }
    });
  }

  //滑动切换触发
  changeSelect() {
    this.tabsActiveIndex = this.slides.getActiveIndex();
    if (this.tabsActiveIndex > 2) this.tabsActiveIndex = 2;
  }

  openTeamDetail(teamcode) {
    this.navCtrl.push("TeamDetailsPage", {
      teamcode
    });
  }

  loginTimeOut() {
    super.showToast(this.toastCtrl, "登录失效，请重新登录");
    this.app.getRootNav().setRoot('LoginOrRegisterPage');
  }

  //slides 切换
  toggle(index) {
    this.tabsActiveIndex = index;
    this.slides.slideTo(index);
  }

  //返回
  back() {
    this.navCtrl.pop();
  }

  //新建团队界面
  openNewTeamPage() {
    //目前所有人都可以创建团队
    this.navCtrl.push("TeamNewPage");

    //判断是否有创建团队的权限 只请求一次
    // if (this.canCreate == null) {
    //   this.teamSeriveProvider.getCanCreate().subscribe(
    //     data => {
    //       if (data.status == 200) {
    //         this.navCtrl.push("TeamNewPage");
    //         this.canCreate = true;
    //       } else if (data.status == 500) {
    //         this.loginTimeOut();
    //       } else {
    //         this.canCreate = false;
    //         super.showToast(this.toastCtrl, "没有新建团队的权限");
    //       }
    //     },
    //     err => {
    //       super.showToast(this.toastCtrl, "请检查网络连接之后重试");
    //     }
    //   );
    //   return;
    // }
    // if (this.canCreate) this.navCtrl.push("TeamNewPage");
    // else super.showToast(this.toastCtrl, "没有新建团队的权限");
  }

  //获取全部团队
  getTeamAll() {
    this.teamSeriveProvider.getTeamAll().subscribe(
      data => {
        if (data != null) {
          this.loading.dismiss();
          if (data.status != 500) {
            this.teamAllDatas = data;


            return;
          } else {
            this.loginTimeOut();
          }
        }
      },
      err => {
        super.showToast(this.toastCtrl, "请检查网络连接之后重试");
      }
    );
  }


}
